(function() {
	'use strict';

	angular.module('milkbeeApp').controller('InstantGiftDialogController',
			InstantGiftDialogController);

	InstantGiftDialogController.$inject = [ '$timeout', '$scope', '$http',
			'$stateParams', '$uibModalInstance', 'entity', 'Gift',
			'ParseLinks', 'AlertService', 'paginationConstants', 'pagingParams' ];

	function InstantGiftDialogController($timeout, $scope, $http, $stateParams,
			$uibModalInstance, entity, Gift, ParseLinks, AlertService,
			paginationConstants, pagingParams) {
		var vm = this;

		vm.gift = entity;
		vm.clear = clear;
		vm.datePickerOpenStatus = {};
		vm.openCalendar = openCalendar;
		vm.userId = $stateParams.userId;
		pagingParams.page = 1;
		loadAll();
		function loadAll() {
			Gift.query({
				page : pagingParams.page - 1,
				size : vm.itemsPerPage,
				sort : sort()
			}, onSuccess, onError);
			function sort() {
				var result = [ 'id', 'desc' ];
				return result;
			}
			function onSuccess(data, headers) {
				vm.links = ParseLinks.parse(headers('link'));
				vm.totalItems = headers('X-Total-Count');
				vm.queryCount = vm.totalItems;
				vm.gifts = data;
				vm.page = pagingParams.page;
			}
			function onError(error) {
				AlertService.error(error.data.message);
			}
		}
		$timeout(function() {

		});

		function clear() {
			$uibModalInstance.dismiss('cancel');
		}

		function onSaveSuccess(result) {
			vm.isSaving = false;
		}

		function onSaveError() {
			vm.isSaving = false;
		}

		vm.datePickerOpenStatus.createdAt = false;

		function openCalendar(date) {
			vm.datePickerOpenStatus[date] = true;
		}
		vm.onGiftChange = function($event, id) {
			var checkbox = $event.target;
			var checked = checkbox.checked;
		}
		vm.sendgift = function(giftId, userId) {
			$http({
				method : 'POST',
				url : '/api/my/gift/send/' + giftId + '/' + userId
			}).success(function(ret) {
				if (ret.success) {
					alert(ret.success);
				} else if (!ret.success) {
					alert(ret.success);
				}
			})

		}
	}
})();
